#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repp(i, n) for(int i = 1; i <= n; ++i)
#define forr(i, l, r) for(int i = l; i <= r; ++i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 2000006;
int n, a[N];
ll val[N], f[N], g[N], ans;
void SOLVE() {
    cin >> n;
    repp(i, n) {
        cin >> a[i];
        val[i] = (a[i] == a[i - 1]) * a[i];
    }
    val[n + 1] = 0;
    ans = 0;
    repp(i, n) f[a[i]] = g[a[i]] = -1e9;
    repp(i, n) {
        if(a[i - 1] == a[i])
            f[a[i]] = ans;
        else 
            f[a[i]] = max(ans, f[a[i]] - val[i] - val[i + 1] + a[i]);
        f[a[i]] = max(f[a[i]], g[a[i]] - val[i] + a[i]);
        g[a[i - 1]] = max(g[a[i - 1]], f[a[i]]);
        ans = max(ans, f[a[i]]);
    }
    repp(i, n) ans += val[i];
    cout << ans << endl;
}
int main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int T;
    cin >> T;
    while(T--) SOLVE();
    return 0;
}